package helper.request.contacts;

import java.sql.SQLException;

import server.dbconnector.DatabaseConnector;
import helper.request.RequestUndo;
import base.Contact;
import base.User;

public class ModifyContactRequest implements ContactRequest,RequestUndo{
	private static final long serialVersionUID = -8633863925529083150L;
	private User currentuser;
	private Contact target;
	private String notes;
	private String altername;
	private boolean allowedtoviewothers;
	private int level;
	private String changenote;
	private int changelevel;
	private String changealtername;
	public ModifyContactRequest(User currentuser,Contact target,String cn,int l,String can){
		this.currentuser=currentuser;
		this.target=target;
		this.notes=target.getnotes();
		this.altername=target.getaltername();
		this.allowedtoviewothers=target.getpermissionforcontact();
		this.level=target.getlevel();
		this.changealtername=can;
		this.changelevel=l;
		this.changenote=cn;
	}
	public void changenote(String target){
		this.target.setnote(target);
	}
	public void changealtername(String altername){
		this.target.setaltername(altername);
	}
	public void changepermission(boolean target){
		this.allowedtoviewothers=target;
	}
	public void setlevel(int level){
		this.target.setlevel(level);
	}
	public int getlevel(){
		return this.level;
	}
	public String getnote(){
		return this.notes;
	}
	public String getaltername(){
		return this.altername;
	}
	public boolean getpermission(){
		return this.allowedtoviewothers;
	}
	public User gettargetuser() {
		return target.gettargetuser();
	}

	@Override
	public int gettargetuserid() {
		return target.gettargetuser().getid();
	}

	@Override
	public User getcurrentuser() {
		return currentuser;
	}

	@Override
	public int getcurrentuserid() {
		return currentuser.getid();
	}

	@Override
	public boolean execute() {
		this.changealtername(changealtername);
		this.changenote(changenote);
		this.setlevel(changelevel);
		
		return true;
	}
	@Override
	public boolean de_execute() {
		this.changealtername(altername);
		this.changenote(notes);
		this.setlevel(level);
		return false;
	}
	@Override
	public void process(DatabaseConnector dc)throws SQLException{
		dc.commit("update contact set notes='"+this.changenote+"' where userIDFrom="+currentuser.getid()+" and userIDTo="+target.gettargetuser().getid());
		dc.commit("update contact set level="+this.changelevel+" where userIDFrom="+currentuser.getid()+" and userIDTo="+target.gettargetuser().getid());
		dc.commit("update contact set altername='"+this.changealtername+"' where userIDFrom="+currentuser.getid()+" and userIDTo="+target.gettargetuser().getid());
	}
}
